﻿using System;
using System.Collections.Generic;
using System.Linq;
using System.Text;
using Model;
using IDAL;
using System.Data.Linq;

namespace SQLServerDAL
{
    public class UserInfoDAL : IUserInfoDAL
    {
        public Boolean InsertUserInfo(UserInfo userInfo) 
        {
            try
            {   
                UserInfoDataClassesDataContext userInfoContext = new UserInfoDataClassesDataContext();
                userInfoContext.UserInfos.InsertOnSubmit(userInfo);
                userInfoContext.SubmitChanges();
                return true;
            }
            catch (Exception)
            {
                return false;
            }
        }


        public Boolean DeleteUserInfo(int userId) 
        {
            try
            {
                UserInfoDataClassesDataContext userInfoContext = new UserInfoDataClassesDataContext();
                UserInfo userInfo = (from u in userInfoContext.UserInfos
                             where u.UserId.Equals(userId)
                             select u).First();
                userInfoContext.UserInfos.DeleteOnSubmit(userInfo);
                userInfoContext.SubmitChanges();
                return true;
            }
            catch (Exception)
            {
                return false;
            }
        }

        public Boolean UpdateUserInfo(int userId, UserInfo userInfo)
        {

            if (userId != userInfo.UserId)
            {
                return false;
            }
            UserInfoDataClassesDataContext userInfoContext = new UserInfoDataClassesDataContext();
            UserInfo query = (from u in userInfoContext.UserInfos
                          where u.UserId.Equals(userId)
                          select u).First();
            //query.Password = user.Password;

            userInfoContext.SubmitChanges();
            return true;

        }


        public UserInfo SelectUserInfoById(int userId)
        {
            UserInfoDataClassesDataContext userInfoContext = new UserInfoDataClassesDataContext();
            UserInfo userInfo = (from u in userInfoContext.UserInfos
                         where u.UserId.Equals(userId)
                         select u).First();
            return userInfo;
        }


        public List<UserInfo> SelectAllUserInfo()
        {
            UserInfoDataClassesDataContext userInfoContext = new UserInfoDataClassesDataContext();
            var query = (from u in userInfoContext.UserInfos
                         select u);
            List<UserInfo> userInfoList = new List<UserInfo>();

            foreach (var userInfo in query)
            {
                userInfoList.Add(userInfo);
            }
            return userInfoList;
        }


    }
}
